package b2.widget
{
	import flash.display.Sprite;

	public class ProgressBar extends Sprite
	{
		//进度0 - 1
		private var _percentage:Number;
		//进度条
		private var progressBarSprite:Sprite;
		//进度条100时候的长度 
		private var completeLength:Number;
		//垂直进度条
		private var verticalFlag:Boolean;
		//空白大小
		private var gap:Number;
		
		public function ProgressBar(CProgressBarFrame:Class, CProgressBar:Class, percentage:Number = 0, gap:Number = 5, verticalFlag:Boolean = false)
		{
			super();
			this.verticalFlag = verticalFlag;
			this.gap = gap;
			//外框
			var progressBarFrameSprite:Sprite = new CProgressBarFrame();
			addChild(progressBarFrameSprite);
			//进度条
			progressBarSprite = new CProgressBar();
			addChild(progressBarSprite);
			progressBarSprite.x = gap;
			progressBarSprite.y = gap;
			if (verticalFlag)
			{
				progressBarSprite.width = progressBarFrameSprite.width - 2 * gap;
				progressBarSprite.height = 0;
				completeLength = progressBarFrameSprite.height - 2 * gap;
			}
			else
			{
				progressBarSprite.width = 0;
				progressBarSprite.height = progressBarFrameSprite.height - 2 * gap;
				completeLength = progressBarFrameSprite.width - 2 * gap;
			}
			this.percentage = percentage;
		}
		
		public function get percentage():Number
		{
			return _percentage; 
		}
		
		public function set percentage(percent:Number):void
		{
			this._percentage = Math.min(Math.max(percent, 0), 1);
			if (verticalFlag)
			{
				progressBarSprite.height = _percentage * completeLength;
				progressBarSprite.y = completeLength + gap - progressBarSprite.height;
			}	
			else
				progressBarSprite.width = _percentage * completeLength;
		}
	}
}